<?xml version="1.0"?>
<xsl:stylesheet version="1.0" 
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:a="http://www.w3.org/2005/Atom"
                xmlns:ac="http://ionrock.org/ns/xsltemplate"
                xmlns:dyn="http://exslt.org/dynamic"
                xmlns:f="http://xmlns.4suite.org/ext"
                xmlns:xh="http://www.w3.org/1999/xhtml"
                xmlns:func="http://exslt.org/functions"
                xmlns="http://www.w3.org/1999/xhtml"
                extension-element-prefixes="f func"
                exclude-result-prefixes="ac f dyn func xh #default">

  <xsl:output method="html" omit-xml-declaration="yes" indent="yes" />
  <xsl:strip-space elements="*" />

  <xsl:param name="ac:base_url">/</xsl:param>
  <xsl:param name="ac:openid" />

  <xsl:param name="ac:updated" select="f:strftime('%Y-%m-%dT%H:%M:%SZ')" />
  <xsl:param name="ac:flash" />
  <xsl:param name="ac:entry_uri" />
  <xsl:param name="ac:collection_uri" />
  <xsl:param name="ac:form_action">.</xsl:param>
  <xsl:param name="ac:atom_title" />
  <xsl:param name="ac:atom_content" />
  <xsl:param name="ac:atom_summary" />
  <xsl:param name="ac:atom_slug" />
  <xsl:param name="ac:atom_updated" />
  <xsl:param name="ac:atom_published" />
  <xsl:param name="ac:err_atom_title" />
  <xsl:param name="ac:err_atom_content" />
  <xsl:param name="ac:err_atom_updated" />
  <xsl:param name="ac:check_params">Nope not working</xsl:param>

  <xsl:template match="/">
    <html xmlns="http://www.w3.org/1999/xhtml">
      <head>
        <title>Atom Publishing Protocol Client</title>
        <xsl:variable name="css" select="concat($ac:base_url, 'static/css/styles.css')" />
        <link rel="stylesheet" type="text/css" href="{$css}" />
        <xsl:variable name="tiny-mce" select="concat($ac:base_url, 'static/js/tiny_mce/tiny_mce.js')" />
        <script type="text/javascript" src="{$tiny-mce}"><xsl:text> </xsl:text></script>
        <xsl:variable name="tiny-mce-init" select="concat($ac:base_url, 'static/js/tiny_mce_init.js')" />
        <script type="text/javascript" src="{$tiny-mce-init}"><xsl:text> </xsl:text></script>
      </head>
      <body>
        <div id="content-block">
          <div id="header-block">
            <h4>WSGIAppClient</h4>
            <div class="subheading">An Atom Publishing Protocol Client</div>
            <div class="breadcrumbs">
              <a href="{ac:this-base-url()}">home</a> &#8594;
              edit
            </div>
          </div>
          <div id="body-block">
            <xsl:if test="$ac:flash">
              <div class="flash-message">
                <xsl:value-of select="$ac:flash" />
              </div>
            </xsl:if>
            <div><xsl:value-of select="check_params" /></div>
            <xsl:apply-templates />
          </div>
        </div>
      </body>
    </html>
  </xsl:template>
  
  <xsl:template match="a:entry">
    <form action="{ac:this-base-url()}" method="POST" enctype="multipart/form-data">
      <fieldset id="main">
       <input type="submit" name="action" value="edit" />
       <input type="submit" name="action" value="delete" />
       <input type="hidden" name="entry_uri" value="{$ac:entry_uri}" />
       <input type="hidden" name="collection_uri" value="{$ac:collection_uri}" />
       <legend>Update an Entry</legend>
        <div>
          <label for="atom_updated">Updated</label>
          <input type="checkbox" name="atom_updated" value="{$ac:updated}" />
        </div>
        <xsl:apply-templates />
      </fieldset>
    </form>
  </xsl:template>

  <!-- hidden items -->
  <xsl:template match="/a:entry/a:summary | /a:entry/a:published">
    <input type="hidden" name="{concat('atom_', local-name())}" value="{.}" />
  </xsl:template>

  <xsl:template match="/a:entry/a:title">
    <xsl:if test="local-name()">
      <xsl:variable name="name" select="concat('atom_', local-name())" />
      <div class="text-input {$name}">
        <label for="{$name}"><xsl:value-of select="local-name()" /></label>
        <input type="text" name="{$name}" value="{.}" />
      </div>
    </xsl:if>
  </xsl:template>
  
  <xsl:template match="/a:entry/a:content | /a:entry/a:summary">
    <xsl:if test="local-name()">
      <xsl:variable name="name" select="concat('atom_', local-name())" />
      <div class="{$name}">
        <label for="{$name}"><xsl:value-of select="local-name()" /></label>
        <xsl:apply-templates select="." mode="text-construct" />
      </div>
    </xsl:if>
  </xsl:template>


  <xsl:template match="* | text()" />

  <!-- grabbed from BC -->
  <xsl:template match="*[@type='text']" mode="text-construct">
    <xsl:variable name="name" select="concat('atom_', local-name())" />
    <br />
    <textarea name="{$name}">
      <xsl:choose>
        <xsl:when test="string-length(.) &gt; 0">
          <xsl:value-of select="node()"/>
        </xsl:when>
        <xsl:otherwise>
          <xsl:text> </xsl:text>
        </xsl:otherwise>
      </xsl:choose>
    </textarea>
  </xsl:template>
  
  <xsl:template match="*[@type='xhtml']" mode="text-construct">
    <xsl:variable name="name" select="concat('atom_', local-name())" />
    <br />
    <textarea name="{$name}">
      <xsl:choose>
        <xsl:when test="count(./xh:div/child::node()) &gt; 1">
          <xsl:copy-of select="./xh:div/*" />
        </xsl:when>
        <xsl:otherwise>
          <xsl:value-of select="./xh:div" /><xsl:text> </xsl:text>
        </xsl:otherwise>
      </xsl:choose>
    </textarea>
  </xsl:template>
  
  <xsl:template match="*[@type='html']" mode="text-construct">
    <xsl:variable name="name" select="concat('atom_', local-name())" />
    <!--xsl:copy-of select="f:parse-xml(node())"/-->
    <br />
    <textarea name="{$name}">
      <xsl:choose>
        <xsl:when test="string-length(.) &gt; 0">
          <xsl:value-of select="bc:unescape(.)" disable-output-escaping="yes"/>
        </xsl:when>
        <xsl:otherwise>
          <xsl:text> </xsl:text>
        </xsl:otherwise>
      </xsl:choose>
    </textarea>
    <!--f:raw-text-output select="string(.)"/-->
  </xsl:template>
  
  <func:function name="ac:this-base-url">
    <xsl:param name="path" />
    <func:result select="f:resolve-url($ac:base_url, $path)" />
  </func:function>


  <!-- Handle content elements with external data -->
  <!--  <xsl:template match="a:content[@src]" mode="text-construct"> -->
  <!-- Use the XHTML object element -->
  <!--   <object data="{@src}" type="{@type}">Unable to render object</object> -->
  <!--  </xsl:template> -->

  <!-- <xsl:template match="node()" mode="copy"> -->
<!--     <xsl:copy> -->
<!--       <xsl:copy-of select="@* | text()" /> -->
<!--       <xsl:apply-templates /> -->
<!--     </xsl:copy> -->
<!--   </xsl:template> -->

</xsl:stylesheet>